import {Model} from "mongoose";
import {Injectable, Inject} from '@nestjs/common';
// 数据库存入数据类型
import {Mongoose} from "./interfaces/mongoose.interface";

// 前台传入参数类型
import {CreateDto} from './dto/create-mongoose.dto'

@Injectable()
export class MongooseService {
    constructor(
        @Inject('MongooseModel')
        private readonly mongooseModel: Model<Mongoose>,) {
    }

    // 创建数据
    async create(createDto: CreateDto): Promise<Mongoose> {
        const doc = new this.mongooseModel(createDto)
        return await doc.save()
    }

    // 查询
    async findAll(): Promise<Mongoose[]> {
        return await this.mongooseModel.find().exec()
    }

    // 根据id查询
    async findById(_id): Promise<Mongoose> {
        return await this.mongooseModel.findById(_id).exec()
    }

}
